<template>
  <el-dialog
      v-model="visible"
      title="修改数据表"
      width="600px"
  >
    <el-form :model="form" label-width="100px">
       <el-form-item label="表ID">
        <el-input v-model="form.tableId" />
      </el-form-item>
      <el-form-item label="表名称">
        <el-input v-model="form.tableName" />
      </el-form-item>
      <el-form-item label="数据库名">
        <el-input v-model="form.databaseName" />
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="form.description" type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit">提交</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import {useTableService} from '@/api/Table/TableApi'

// 在组件中使用表格服务
const { updateTable, loading: createLoading } = useTableService()

const visible = ref(false)
const form = ref({
  tableId: 0,
  tableName: '',
  databaseName: '',
  description: ''
})

const emit = defineEmits(['success'])
// 修改表信息
const handleSubmit = async () => {
  const response = await updateTable({
    tableId: form.value.tableId,
    tableName: form.value.tableName,
    databaseName: form.value.databaseName,
    description: form.value.description
  })
  // 触发成功事件，传递新增的表格数据
  emit('success', response.data)
  // 关闭对话框
  visible.value = false
  // 重置表单
  form.value = {
    tableId: 0,
    tableName: '',
    databaseName: '',
    description: ''
  }
  ElMessage.success("修改成功")
}


// 暴露打开方法
const open = (tableId?: number) => {
  visible.value = true
}

defineExpose({ open })
</script>
